package com.ziyear.servlet;

import com.ziyear.common.constant.TrainConstant;
import com.ziyear.domain.Train;
import com.ziyear.service.TrainService;
import com.ziyear.service.impl.TrainServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * 功能描述 : 管理员  添加车次的Servlet
 *
 * @author Ziyear
 */
public class AddTrainServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddTrainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         * 	获取界面中输入的数据
         */
        String trainNumber = request.getParameter("trainNumber");
        String startStation = request.getParameter("startStation");
        String endStation = request.getParameter("endStation");
        String startTime = request.getParameter("startTime");
        startTime = startTime.substring(0, 10) + " " + startTime.substring(11) + ":00";
        String endTime = request.getParameter("endTime");
        endTime = endTime.substring(0, 10) + " " + endTime.substring(11) + ":00";
        Integer shangWuCarsNum = Integer.valueOf(request.getParameter("shangWuCarsNum"));
        BigDecimal shangWuCarsPrice = new BigDecimal(request.getParameter("shangWuPrice"));
        Integer yiDengCarsNum = Integer.valueOf(request.getParameter("yiDengCarsNum"));
        BigDecimal yiDengCarsPrice = new BigDecimal(request.getParameter("yiDengPrice"));
        Integer erDengCarsNum = Integer.valueOf(request.getParameter("erDengCarsNum"));
        BigDecimal erDengCarsPrice = new BigDecimal(request.getParameter("erDengPrice"));

        // 获取当前时间
        Timestamp addTime = new Timestamp(System.currentTimeMillis());

        /**
         * 	将数据封装成对象
         */
        Train train = new Train();
        train.setTrainNumber(trainNumber);
        train.setStartStation(startStation);
        train.setEndStation(endStation);
        train.setStartTime(startTime);
        train.setEndTime(endTime);
        train.setAddTime(addTime);
        train.setShangWuPrice(shangWuCarsPrice);
        train.setYiDengPrice(yiDengCarsPrice);
        train.setErDengPrice(erDengCarsPrice);
        train.setShangWu(shangWuCarsNum * TrainConstant.SHANGWU_CAR_SEAT_NUM);
        train.setYiDeng(yiDengCarsNum * TrainConstant.YIDENG_CAR_SEAT_NUM);
        train.setErDeng(erDengCarsNum * TrainConstant.ERDENG_CAR_SEAT_NUM);
        train.setShangWuNum(shangWuCarsNum);
        train.setYiDengNum(yiDengCarsNum);
        train.setErDengNum(erDengCarsNum);
        /**
         * 	将数据存入数据库,并返回是否成功的结果
         */
        TrainService trainService = new TrainServiceImpl();
        int recordNumber = trainService.add(train);

        /**
         * 	根据返回值判断是否添加车次成功，返回1为成功
         */
        PrintWriter out = response.getWriter();
        if (recordNumber == 1) {
            out.write("<script>alert('添加成功！');"
                    + "window.location.href='GetAllTrainServlet'</script>");
        } else {
            out.write("<script>alert('很抱歉,添加失败！');"
                    + "window.location.href='pages/admin/right/addTrain.jsp'</script>");
        }

        out.close();
        //response.sendRedirect("pages/admin/right/addTrain.jsp");

    }

}
